﻿<cfscript>
/** 
* @hint "Entity: ProgramTask, CommonTask"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.taskMarkResitListener" 
	output=false 
	{
		
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "解锁补考成绩提交任务"
	* 
	* @event "MachII Event Object"
	*/	
	public void function unlockTaskMark( required MachII.framework.Event event ) output=false {
		
		var taskId = event.getArg("TSK");
		
		var sql = queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
		
			sql = "	SELECT
						t.task_id
					FROM
						t_resitplan t
					WHERE
						t.task_id = :taskId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
			rs_markTaskEntity = queryObj.execute(sql = sql).getResult();

			//if ( not isNull(markTask) ) {
			if( rs_markTaskEntity.recordCount ){

				/* 第一步 追加学生名单 */
				sql = "	SELECT
							a.scc_id,
							a.task_type,
							a.retaking,
							a.mark_mode,
							a.mark_resit,
							a.mark_renote,
							a.stu_id,
							a.cid,
							a.term_id,
							a.mark_resit_limit
						FROM
							t_student_mark a
						WHERE
							a.task_id = :taskId 
							AND
							(a.mark_resit > -1 OR a.mark_renote = '2' OR a.mark_renote = '9')
							AND
							NOT EXISTS(
								SELECT 1 FROM t_temp_mark_resit b WHERE a.scc_id = b.scc_id AND b.task_id = :taskId 
							)
						ORDER BY
							a.stu_id";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
				rs_chooserAdd = queryObj.execute(sql = sql).getResult();
				
				for( a = 1; a le rs_chooserAdd.recordCount; a++ ){

					sql = "	INSERT INTO t_temp_mark_resit(
								scc_id,
								stu_id,
								term_id,
								cid,
								task_type,
								task_id,
								retaking,
								mark_mode,
								mark_resit,
								mark_renote,
								mark_limite
							)VALUES(
								:chooserId,
								:studentId,
								:termId,
								:courseId,
								:taskType,
								:taskId,
								:retaking,
								:markMode,
								:markResit,
								:markRenote,
								:markLimite 
							)";
					
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "chooserId", value = rs_chooserAdd["scc_id"][a], cfsqltype = "cf_sql_varchar");
					queryObj.addParam(name = "studentId", value = rs_chooserAdd["stu_id"][a], cfsqltype = "cf_sql_varchar");
					queryObj.addParam(name = "termId", value = rs_chooserAdd["term_id"][a], cfsqltype = "cf_sql_char");
					queryObj.addParam(name = "courseId", value = rs_chooserAdd["cid"][a], cfsqltype = "cf_sql_varchar");
					queryObj.addParam(name = "taskType", value = rs_chooserAdd["task_type"][a], cfsqltype = "cf_sql_char");
					queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
					queryObj.addParam(name = "retaking", value = rs_chooserAdd["retaking"][a], cfsqltype = "cf_sql_char");
					queryObj.addParam(name = "markMode", value = rs_chooserAdd["mark_mode"][a], cfsqltype = "cf_sql_char");
					queryObj.addParam(name = "markResit", value = rs_chooserAdd["mark_resit"][a], cfsqltype = "cf_sql_decimal", scale = 2 );
					queryObj.addParam(name = "markRenote", value = rs_chooserAdd["mark_renote"][a], cfsqltype = "cf_sql_char");
					queryObj.addParam(name = "markLimite", value = rs_chooserAdd["mark_resit_limit"][a], cfsqltype = "cf_sql_decimal", scale = 2 );
					queryObj.execute(sql = sql);
				}
					

					
				/* 第四步 更新成绩提交数量 */
				sql = "	SELECT
							COUNT(*) mark_need,
							SUM(
								CASE
									WHEN t.mark_renote = '1' AND t.mark_resit >= 0 THEN 1
									WHEN t.mark_renote != '1' THEN 1
									ELSE 0
								END
							) mark_done
						FROM
							t_temp_mark_resit t
						WHERE
							t.task_id = :taskId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
				rs_checkQuery = queryObj.execute(sql = sql).getResult();

				/* 第五步 标准任务为解锁状态 */

				sql = "	UPDATE t_resitplan p
							SET
								p.mark_need = :markNeed,
								p.mark_done = :markDone,
								p.task_prog = '4' 
							WHERE
								p.task_id = :taskId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "markNeed", value = rs_checkQuery.mark_need, cfsqltype = "cf_sql_integer");
				queryObj.addParam(name = "markDone", value = rs_checkQuery.mark_done, cfsqltype = "cf_sql_integer");
				queryObj.execute(sql = sql);
				
			}				
		}			
	}

}

</cfscript>